package com.longge.wechat.service.impl;


import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Propagation;

import com.longge.wechat.dao.UserMessageDao;
import com.longge.wechat.entity.UserMessageEntity;
import com.longge.wechat.service.UserMessageService;
import com.longge.wechat.utils.XmlUtil;


@Service
@Transactional(propagation = Propagation.REQUIRED)
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)
public class UserMessageServiceImpl implements UserMessageService {
	@Resource
	private UserMessageDao userMessageDaoImpl; 

	@Override
	public void saveUserMessage(UserMessageEntity ume) {
		userMessageDaoImpl.saveUserMessage(ume);
	}
	
	public void xml2Object(XmlUtil xmlUtil) {
		UserMessageEntity ume = new UserMessageEntity();
        ume.setOpenId(xmlUtil.getContentByNodeName("FromUserName"));
        String typeName = xmlUtil.getContentByNodeName("MsgType");
        ume.setMsgType(typeName);
        ume.setCreateTime(new Date());
        if("image".equals(typeName)) {
        	ume.setMediaId1(xmlUtil.getContentByNodeName("MediaId"));
        }else if("voice".equals(typeName)) {
        	ume.setMediaId2(xmlUtil.getContentByNodeName("MediaId"));
        	ume.setContent(xmlUtil.getContentByNodeName("Recognition"));
        }else if ("event".equals(xmlUtil.getContentByNodeName("MsgType"))){
        	if("LOCATION".equals(typeName)) {
        		ume.setX(Float.parseFloat(xmlUtil.getContentByNodeName("Latitude")));
        		ume.setY(Float.parseFloat(xmlUtil.getContentByNodeName("Longitude")));
        	}
        }
        userMessageDaoImpl.saveUserMessage(ume);
	}

	@Override
	public void updateUserMessage(UserMessageEntity ume) {
		userMessageDaoImpl.updateUserMessage(ume);
	}

	@Override
	public UserMessageEntity findUserMessageByOpenId(String openId) {
		return userMessageDaoImpl.findUserMessageByOpenId(openId);
	}

	@Override
	public List<UserMessageEntity> findAllUserMessage(int pageSize, int currentPage) {
		return userMessageDaoImpl.findAllUserMessage(pageSize,currentPage);
	}
}
